﻿Imports DTO
Imports System.Data.SqlClient

Public Class KhoHangDAO
    Public Shared Function LoadBangKhoHang() As DataTable
        Return CKetNoi.LoadBang("usp_LoadBangKhoHang")
    End Function

    Public Shared Function LayDSKhoHang() As DataTable
        Return CKetNoi.GetData("LayDSKhoHang")
    End Function

    Public Shared Function ThemKhoHang(kh As KhoHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@tenKH", kh.TenKhoHang))
        lst.Add(New SqlParameter("@kyhieu", kh.KyHieu))
        If kh.MaNguoiQL = Nothing Then
            lst.Add(New SqlParameter("@maQL", DBNull.Value))
        Else
            lst.Add(New SqlParameter("@maQL", kh.MaNguoiQL))
        End If
        lst.Add(New SqlParameter("@fax", kh.Fax))
        lst.Add(New SqlParameter("@email", kh.Email))
        lst.Add(New SqlParameter("@ghichu", kh.DienGia))
        lst.Add(New SqlParameter("@maTT", kh.MaTrangThai))
        lst.Add(New SqlParameter("@dt", kh.DienThoai))
        lst.Add(New SqlParameter("@nlh", kh.NguoiLienHe))
        lst.Add(New SqlParameter("@dc", kh.DiaChi))
        kt = CKetNoi.ExecQueryStoreProc("ThemKhoHang", lst)
        Return kt
    End Function

    Public Shared Function CapNhatKhoHang(kh As KhoHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@maKH", kh.MaKhoHang))
        lst.Add(New SqlParameter("@tenKH", kh.TenKhoHang))
        lst.Add(New SqlParameter("@kyhieu", kh.KyHieu))
        If kh.MaNguoiQL = Nothing Then
            lst.Add(New SqlParameter("@maQL", DBNull.Value))
        Else
            lst.Add(New SqlParameter("@maQL", kh.MaNguoiQL))
        End If
        lst.Add(New SqlParameter("@dt", kh.DienThoai))
        lst.Add(New SqlParameter("@nlh", kh.NguoiLienHe))
        lst.Add(New SqlParameter("@fax", kh.Fax))
        lst.Add(New SqlParameter("@ghichu", kh.DienGia))
        lst.Add(New SqlParameter("@email", kh.Email))
        lst.Add(New SqlParameter("@maTT", kh.MaTrangThai))
        lst.Add(New SqlParameter("@dc", kh.DiaChi))

        kt = CKetNoi.ExecQueryStoreProc("CapNhatKhoHang", lst)
        Return kt
    End Function

    Public Shared Function XoaKhoHang(kh As KhoHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@maKH", kh.MaKhoHang))

        kt = CKetNoi.ExecQueryStoreProc("XoaKhoHang", lst)
        Return kt
    End Function

    Public Shared DongKetNoi As SqlConnection = CKetNoi.Connect

    Public Shared Sub LayMaKhoHangCbx(ByVal makh As String, ByRef maql As String)

        DongKetNoi.Open()

        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "LayMaKhoHangCbx"
        cmd.Connection = DongKetNoi


        Dim para As SqlParameter

        para = New System.Data.SqlClient.SqlParameter("makh", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Input
        para.Value = makh
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("maql", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        cmd.ExecuteNonQuery()

        maql = cmd.Parameters("maql").Value

        DongKetNoi.Close()

    End Sub

End Class
